home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / hashsig / hashsig.lha / md4Block.c < prev    next >
Text File  |  1993-08-09  |  17KB  |  511 lines

  1. /*
  2.  
  3. This version of the algorithm has been modified by Ralph C. Merkle
  4. 90.03.12.  Basically, various portions of the general-purpose hash
  5. provided by Ron Rivest and RSA Data Security have been removed,
  6. leaving only the guts of the algorithm for use as a simple "block
  7. hash" algorithm.
  8.  
  9. The following notice came from the version posted to sci.crypt
  10. by RSA Data Security:
  11.  
  12. License to copy and use this document and the software described
  13. herein is granted provided it is identified as the "RSA Data
  14. Security, Inc. MD4 Message Digest Algorithm" in all materials
  15. mentioning or referencing this software, function, or document.
  16.  
  17. License is also granted to make derivative works provided that such
  18. works are identified as "derived from the RSA Data Security, Inc. MD4
  19. Message Digest Algorithm" in all material mentioning or referencing
  20. the derived work.
  21.  
  22. RSA Data Security, Inc. makes no representations concerning the
  23. merchantability of this algorithm or software or their suitability
  24. for any specific purpose.  It is provided "as is" without express or
  25. implied warranty of any kind.
  26.  
  27. These notices must be retained in any copies of any part of this
  28. documentation and/or software.
  29. ------------------------------------------------------------------
  30.  * XEROX GRANTS NO DIFFERENT OR ADDITIONAL RIGHTS TO USE THE
  31.  * MODIFIED VERSION OF MD4 CONTAINED HEREIN.
  32.  
  33. */
  34.  
  35. /*  The original comments, documentation, and explanations have
  36.     been retained, but they no longer are fully applicable to the
  37.     "stripped" version of md4 actually coded here.
  38. */
  39.  
  40. #if 0
  41.  
  42. ------------------------------------------------------------------------
  43. ------------------------------------------------------------------------
  44.  
  45.          The MD4 Message Digest Algorithm
  46.          --------------------------------
  47.             by Ronald L. Rivest
  48.     MIT Laboratory for Computer Science, Cambridge, Mass. 02139
  49.                 and
  50.     RSA Data Security, Inc., Redwood City, California 94065
  51.             (Version 2/17/90 -- Revised)
  52.  
  53.  
  54. Abstract:
  55. ---------
  56.                 
  57. This note describes the MD4 message digest algorithm.  The algorithm
  58. takes as input an input message of arbitrary length and produces as
  59. output a 128-bit ``fingerprint'' or ``message digest'' of the input.
  60. It is conjectured that it is computationally infeasible to produce two
  61. messages having the same message digest, or to produce any message
  62. having a given prespecified target message digest.  The MD4 algorithm
  63. is thus ideal for digital signature applications, where a large file
  64. must be ``compressed'' in a secure manner before being signed with the
  65. RSA public-key cryptosystem.
  66.  
  67. The MD4 algorithm is designed to be quite fast on 32-bit machines.  On
  68. a SUN Sparc station, MD4 runs at 1,450,000 bytes/second.  On a DEC
  69. MicroVax II, MD4 runs at approximately 70,000 bytes/second.  On a 20MHz
  70. 80286, MD4 runs at approximately 32,000 bytes/second.  In addition, the
  71. MD4 algorithm does not require any large substitution tables; the
  72. algorithm can be coded quite compactly.
  73.  
  74. The MD4 algorithm is being placed in the public domain for review and
  75. possible adoption as a standard.  
  76.  
  77. (Note: The document supersedes an earlier draft.  The algorithm described
  78.        here is a slight modification of the one described in the draft.)
  79.  
  80.  
  81. I. Terminology and Notation
  82. ---------------------------
  83.  
  84. In this note a ``word'' is a 32-bit quantity and a byte is an 8-byte
  85. quantity.  A sequence of bits can be interpreted in a natural manner
  86. as a sequence of bytes, where each consecutive group of 8 bits is
  87. interpreted as a byte with the high-order (most significant) bit of
  88. each byte listed first.  Similarly, a sequence of bytes can be
  89. interpreted as a sequence of 32-bit words, where each consecutive
  90. group of 4 bytes is interpreted as a word with the low-order (least
  91. significant) byte given first.
  92.  
  93. Let x_i denote ``x sub i''.  If the subscript is an expression, we
  94. surround it in braces, as in x_{i+1}.  Similarly, we use ^ for
  95. superscripts (exponentiation), so that x^i denotes x to the i-th
  96. power.
  97.  
  98. Let the symbol ``+'' denote addition of words (i.e., modulo-2^32
  99. addition). Let X <<< s denote the 32-bit value obtained by circularly
  100. shifting (rotating) X left by s bit positions.  Let not(X) denote the
  101. bit-wise complement of X, and let X v Y denote the bit-wise OR of X
  102. and Y.  Let X xor Y denote the bit-wise XOR of X and Y, and let XY
  103. denote the bit-wise AND of X and Y.
  104.  
  105.  
  106. II. MD4 Algorithm Description
  107. -----------------------------
  108.  
  109. We begin by supposing that we have a b-bit message as input, and
  110. that we wish to find its message digest.  Here b is an arbitrary
  111. nonnegative integer; b may be zero, it need not be a multiple of 8,
  112. and it may be arbitrarily large. We imagine the bits of the message
  113. written down as follows:
  114.  
  115.     m_0 m_1 ... m_{b-1} .
  116.  
  117. The following five steps are performed to compute the message digest of the
  118. message.
  119.  
  120.  
  121. Step 1. Append padding bits
  122. ---------------------------
  123.  
  124. The message is ``padded'' (extended) so that its length (in bits) is
  125. congruent to 448, modulo 512.  That is, the message is extended so
  126. that it is just 64 bits shy of being a multiple of 512 bits long.
  127. Padding is always performed, even if the length of the message is
  128. already congruent to 448, modulo 512 (in which case 512 bits of
  129. padding are added).
  130.  
  131. Padding is performed as follows: a single ``1'' bit is appended to the
  132. message, and then enough zero bits are appended so that the length in bits
  133. of the padded message becomes congruent to 448, modulo 512.
  134.  
  135.  
  136. Step 2. Append length
  137. ---------------------
  138.  
  139. A 64-bit representation of b (the length of the message before the
  140. padding bits were added) is appended to the result of the previous
  141. step.  In the unlikely event that b is greater than 2^64, then only
  142. the low-order 64 bits of b are used.  (These bits are appended as two
  143. 32-bit words and appended low-order word first in accordance with the
  144. previous conventions.)
  145.  
  146. At this point the resulting message (after padding with bits and with
  147. b) has a length that is an exact multiple of 512 bits.  Equivalently,
  148. this message has a length that is an exact multiple of 16 (32-bit)
  149. words.  Let M[0 ... N-1] denote the words of the resulting message,
  150. where N is a multiple of 16.
  151.  
  152.  
  153. Step 3. Initialize MD buffer
  154. ----------------------------
  155.  
  156. A 4-word buffer (A,B,C,D) is used to compute the message digest.  Here
  157. each of A,B,C,D are 32-bit registers.  These registers are initialized
  158. to the following values (in hexadecimal, low-order bytes first):
  159.  
  160.     word A:    01 23 45 67
  161.     word B:    89 ab cd ef
  162.     word C:    fe dc ba 98
  163.     word D:    76 54 32 10
  164.  
  165.  
  166. Step 4. Process message in 16-word blocks
  167. -----------------------------------------
  168.  
  169. We first define three auxiliary functions that each take as input
  170. three 32-bit words and produce as output one 32-bit word.
  171.  
  172.     f(X,Y,Z)  =  XY v not(X)Z 
  173.     g(X,Y,Z)  =  XY v XZ v YZ 
  174.     h(X,Y,Z)  =  X xor Y xor Z 
  175.  
  176. In each bit position f acts as a conditional: if x then y else z.
  177. (The function f could have been defined using + instead of v since XY
  178. and not(X)Z will never have 1's in the same bit position.)  In each
  179. bit position g acts as a majority function: if at least two of x, y, z
  180. are on, then g has a one in that bit position, else g has a zero. It
  181. is interesting to note that if the bits of X, Y, and Z are independent
  182. and unbiased, the each bit of f(X,Y,Z) will be independent and
  183. unbiased, and similarly each bit of g(X,Y,Z) will be independent and
  184. unbiased.  The function h is the bit-wise ``xor'' or ``parity'' function;
  185. it has properties similar to those of f and g.
  186.  
  187. Do the following:
  188.  
  189. For i = 0 to N/16-1 do    /* process each 16-word block */
  190.     For j = 0 to 15 do: /* copy block i into X */
  191.       Set X[j] to M[i*16+j].
  192.     end /* of loop on j */
  193.     Save A as AA, B as BB, C as CC, and D as DD.
  194.  
  195.     [Round 1]
  196.       Let [A B C D i s] denote the operation
  197.         A = (A + f(B,C,D) + X[i]) <<< s  .
  198.       Do the following 16 operations:
  199.         [A B C D 0 3] 
  200.         [D A B C 1 7] 
  201.         [C D A B 2 11] 
  202.         [B C D A 3 19] 
  203.         [A B C D 4 3] 
  204.         [D A B C 5 7] 
  205.         [C D A B 6 11] 
  206.         [B C D A 7 19] 
  207.         [A B C D 8 3] 
  208.         [D A B C 9 7] 
  209.         [C D A B 10 11] 
  210.         [B C D A 11 19] 
  211.         [A B C D 12 3] 
  212.         [D A B C 13 7] 
  213.         [C D A B 14 11] 
  214.         [B C D A 15 19] 
  215.  
  216.     [Round 2]
  217.       Let [A B C D i s] denote the operation
  218.             A = (A + g(B,C,D) + X[i] + 5A827999) <<< s .
  219.       (The value 5A..99 is a hexadecimal 32-bit constant, written with
  220.       the high-order digit first. This constant represents the square
  221.       root of 2.  The octal value of this constant is 013240474631.
  222.           See Knuth, The Art of Programming, Volume 2
  223.       (Seminumerical Algorithms), Second Edition (1981), Addison-Wesley.
  224.       Table 2, page 660.)
  225.       Do the following 16 operations:
  226.         [A B C D 0  3] 
  227.         [D A B C 4  5] 
  228.         [C D A B 8  9] 
  229.         [B C D A 12 13] 
  230.         [A B C D 1  3] 
  231.         [D A B C 5  5] 
  232.         [C D A B 9  9] 
  233.         [B C D A 13 13] 
  234.         [A B C D 2  3] 
  235.         [D A B C 6  5] 
  236.         [C D A B 10 9] 
  237.         [B C D A 14 13] 
  238.         [A B C D 3  3] 
  239.         [D A B C 7  5] 
  240.         [C D A B 11 9] 
  241.         [B C D A 15 13] 
  242.  
  243.     [Round 3]
  244.       Let [A B C D i s] denote the operation
  245.         A = (A + h(B,C,D) + X[i] + 6ED9EBA1) <<< s
  246.       (The value 6E..A1 is a hexadecimal 32-bit constant, written with
  247.       the high-order digit first. This constant represents the square
  248.       root of 3.  The octal value of this constant is 015666365641.
  249.           See Knuth, The Art of Programming, Volume 2
  250.       (Seminumerical Algorithms), Second Edition (1981), Addison-Wesley.
  251.       Table 2, page 660.)
  252.       Do the following 16 operations:
  253.         [A B C D 0  3] 
  254.         [D A B C 8  9] 
  255.         [C D A B 4  11] 
  256.         [B C D A 12 15] 
  257.         [A B C D 2  3] 
  258.         [D A B C 10 9] 
  259.         [C D A B 6  11] 
  260.         [B C D A 14 15] 
  261.         [A B C D 1  3] 
  262.         [D A B C 9  9] 
  263.         [C D A B 5  11] 
  264.         [B C D A 13 15] 
  265.         [A B C D 3  3] 
  266.         [D A B C 11 9] 
  267.         [C D A B 7  11] 
  268.         [B C D A 15 15] 
  269.  
  270. Then perform the following additions:
  271.         A = A + AA
  272.         B = B + BB
  273.         C = C + CC
  274.         D = D + DD
  275. (That is, each of the four registers is incremented by the value it had
  276. before this block was started.)
  277.  
  278. end /* of loop on i */
  279.  
  280.  
  281. Step 5. Output
  282. --------------
  283.  
  284. The message digest produced as output is A,B,C,D.
  285. That is, we begin with the low-order byte of A, and end with the
  286. high-order byte of D.
  287.  
  288. This completes the description of MD4.  A reference implementation in
  289. C is given in the Appendix.
  290.  
  291.  
  292. III. Extensions
  293. ---------------
  294.  
  295. If more than 128 bits of output are required, then the following
  296. procedure is recommended to obtain a 256-bit output.  (There is no
  297. provision made for obtaining more than 256 bits.)
  298.  
  299. Two copies of MD4 are run in parallel over the input.  The first copy
  300. is standard as described above.  The second copy is modified as follows.
  301.  
  302. The initial state of the second copy is:
  303.     word A:    00 11 22 33
  304.     word B:    44 55 66 77
  305.     word C:    88 99 aa bb
  306.     word D:    cc dd ee ff
  307.  
  308. The magic constants in rounds 2 and 3 for the second copy of MD4 are
  309. changed from sqrt(2) and sqrt(3) to cuberoot(2) and cuberoot(3):
  310.                 Octal        Hex
  311.     Round 2 constant    012050505746    50a28be6 
  312.     Round 3 constant    013423350444    5c4dd124
  313.  
  314. Finally, after every 16-word block is processed (including the last
  315. block), the values of the A registers in the two copies are exchanged.
  316.  
  317. The final message digest is obtaining by appending the result of the
  318. second copy of MD4 to the end of the result of the first copy of MD4.
  319.  
  320.  
  321. IV. Summary
  322. ------------
  323.  
  324. The MD4 message digest algorithm is simple to implement, and provides
  325. a ``fingerprint'' or message digest of a message of arbitrary length.
  326.  
  327. It is conjectured that the difficulty of coming up with two messages
  328. having the same message digest is on the order of 2^64 operations, and
  329. that the difficulty of coming up with any message having a given
  330. message digest is on the order of 2^128 operations.  The MD4 algorithm
  331. has been carefully scrutinized for weaknesses.  It is, however, a
  332. relatively new algorithm and further security analysis is of course
  333. justified, as is the case with any new proposal of this sort.  The
  334. level of security provided by MD4 should be sufficient for
  335. implementing very high security hybrid digital signature schemes based
  336. on MD4 and the RSA public-key cryptosystem.
  337.  
  338. V. Acknowledgements
  339. -------------------
  340.  
  341. I'd like to thank Don Coppersmith, Burt Kaliski, Ralph Merkle, and
  342. Noam Nisan for numerous helpful comments and suggestions.
  343.  
  344.  
  345. APPENDIX. Reference Implementation
  346. ----------------------------------
  347.  
  348. This appendix contains the following files:
  349.     md4.h        -- a header file for using the MD4 implementation
  350.     md4.c        -- the source code for the MD4 routines
  351.     md4driver.c    -- a sample ``user'' routine
  352.     session        -- sample results of running md4driver
  353. #endif
  354.  
  355.  
  356. /*
  357. **
  358. **  The following code has been modified (mostly by deleting parts
  359.  The routine Md4Block
  360. **  is designed to hash a single block, and cuts out all
  361. **  the stuff required for hashing inputs of arbitrary lengths.
  362. **  This makes it much faster and simpler for this application.
  363. **
  364. **  Modified 90.03.12 by Ralph C. Merkle
  365. */
  366.  
  367. /*  Original header and credit to RSA Data Security and Ron Rivest  */
  368. /*
  369. ** **************************************************************************
  370. ** md4.c -- Implementation of MD4 Message Digest Algorithm                 **
  371. ** Updated: 2/16/90 by Ronald L. Rivest                                    **
  372. ** (C) 1990 RSA Data Security, Inc.                                        **
  373. ** **************************************************************************
  374. */
  375.  
  376.  
  377. /* Compile-time declarations of MD4 ``magic constants''.
  378. */
  379. #define I0  0x67452301       /* Initial values for MD buffer */
  380. #define I1  0xefcdab89
  381. #define I2  0x98badcfe
  382. #define I3  0x10325476
  383. #define C2  013240474631     /* round 2 constant = sqrt(2) in octal */
  384. #define C3  015666365641     /* round 3 constant = sqrt(3) in octal */
  385. /* C2 and C3 are from Knuth, The Art of Programming, Volume 2
  386. ** (Seminumerical Algorithms), Second Edition (1981), Addison-Wesley.
  387. ** Table 2, page 660.
  388. */
  389. #define fs1  3               /* round 1 shift amounts */
  390. #define fs2  7   
  391. #define fs3 11  
  392. #define fs4 19  
  393. #define gs1  3               /* round 2 shift amounts */
  394. #define gs2  5   
  395. #define gs3  9   
  396. #define gs4 13  
  397. #define hs1  3               /* round 3 shift amounts */
  398. #define hs2  9 
  399. #define hs3 11 
  400. #define hs4 15
  401.  
  402.  
  403. /* Compile-time macro declarations for MD4.
  404. ** Note: The ``rot'' operator uses the variable ``tmp''.
  405. ** It assumes tmp is declared as unsigned, so that the >>
  406. ** operator will shift in zeros rather than extending the sign bit.
  407. */
  408. #define    f(X,Y,Z)             ((X&Y) | ((~X)&Z))
  409. #define    g(X,Y,Z)             ((X&Y) | (X&Z) | (Y&Z))
  410. #define h(X,Y,Z)             (X^Y^Z)
  411. #define rot(X,S)             (tmp=X,(tmp<<S) | (tmp>>(32-S)))
  412. #define ff(A,B,C,D,i,s)      A = rot((A + f(B,C,D) + X[i]),s)
  413. #define gg(A,B,C,D,i,s)      A = rot((A + g(B,C,D) + X[i] + C2),s)
  414. #define hh(A,B,C,D,i,s)      A = rot((A + h(B,C,D) + X[i] + C3),s)
  415.  
  416. typedef unsigned long int word32;
  417.  
  418. extern void ErrAbort();
  419.  
  420. void 
  421. Md4Block(output, outputSize, input, inputSize)
  422. word32 output[];
  423. int outputSize;
  424. word32 input[];
  425. int inputSize;
  426. {
  427.   int i;
  428.   word32 X[16];
  429.   register word32 tmp, A, B, C, D;
  430.  
  431. /*  copy input into X vector */
  432.   for(i=0; i<inputSize; i++)
  433.     X[i] = input[i];
  434.  
  435. /*  zero out rest of X vector */
  436.   for(   ; i<16; i++)
  437.     X[i] = 0;
  438.  
  439.   A = I0;
  440.   B = I1;
  441.   C = I2;
  442.   D = I3;
  443.   /* Update the message digest buffer */
  444.   ff(A , B , C , D ,  0 , fs1); /* Round 1 */
  445.   ff(D , A , B , C ,  1 , fs2); 
  446.   ff(C , D , A , B ,  2 , fs3); 
  447.   ff(B , C , D , A ,  3 , fs4); 
  448.   ff(A , B , C , D ,  4 , fs1); 
  449.   ff(D , A , B , C ,  5 , fs2); 
  450.   ff(C , D , A , B ,  6 , fs3); 
  451.   ff(B , C , D , A ,  7 , fs4); 
  452.   ff(A , B , C , D ,  8 , fs1); 
  453.   ff(D , A , B , C ,  9 , fs2); 
  454.   ff(C , D , A , B , 10 , fs3); 
  455.   ff(B , C , D , A , 11 , fs4); 
  456.   ff(A , B , C , D , 12 , fs1); 
  457.   ff(D , A , B , C , 13 , fs2); 
  458.   ff(C , D , A , B , 14 , fs3); 
  459.   ff(B , C , D , A , 15 , fs4); 
  460.   gg(A , B , C , D ,  0 , gs1); /* Round 2 */
  461.   gg(D , A , B , C ,  4 , gs2); 
  462.   gg(C , D , A , B ,  8 , gs3); 
  463.   gg(B , C , D , A , 12 , gs4); 
  464.   gg(A , B , C , D ,  1 , gs1); 
  465.   gg(D , A , B , C ,  5 , gs2); 
  466.   gg(C , D , A , B ,  9 , gs3); 
  467.   gg(B , C , D , A , 13 , gs4); 
  468.   gg(A , B , C , D ,  2 , gs1); 
  469.   gg(D , A , B , C ,  6 , gs2); 
  470.   gg(C , D , A , B , 10 , gs3); 
  471.   gg(B , C , D , A , 14 , gs4); 
  472.   gg(A , B , C , D ,  3 , gs1); 
  473.   gg(D , A , B , C ,  7 , gs2); 
  474.   gg(C , D , A , B , 11 , gs3); 
  475.   gg(B , C , D , A , 15 , gs4);  
  476.   hh(A , B , C , D ,  0 , hs1); /* Round 3 */
  477.   hh(D , A , B , C ,  8 , hs2); 
  478.   hh(C , D , A , B ,  4 , hs3); 
  479.   hh(B , C , D , A , 12 , hs4); 
  480.   hh(A , B , C , D ,  2 , hs1); 
  481.   hh(D , A , B , C , 10 , hs2); 
  482.   hh(C , D , A , B ,  6 , hs3); 
  483.   hh(B , C , D , A , 14 , hs4); 
  484.   hh(A , B , C , D ,  1 , hs1); 
  485.   hh(D , A , B , C ,  9 , hs2); 
  486.   hh(C , D , A , B ,  5 , hs3); 
  487.   hh(B , C , D , A , 13 , hs4); 
  488.   hh(A , B , C , D ,  3 , hs1); 
  489.   hh(D , A , B , C , 11 , hs2); 
  490.   hh(C , D , A , B ,  7 , hs3); 
  491.   hh(B , C , D , A , 15 , hs4);
  492.  
  493. /*  dropped the increment, too.  For a single block, it doesn't
  494. **  make any difference.  RCM.
  495. */
  496.  
  497.   if(outputSize == 0) return;
  498.   output[0] = A;
  499.   if(outputSize == 1) return;
  500.   output[1] = B;
  501.   if(outputSize == 2) return;
  502.   output[2] = C;
  503.   if(outputSize == 3) return;
  504.   output[3] = D;
  505.   if(outputSize == 4) return;
  506.   ErrAbort("bad outputSize to md4");
  507. }
  508.  
  509.  
  510.  
  511.